package com.ihr360.payroll.service.salarytax.impl;

import com.google.common.collect.Lists;
import com.ihr360.alps.PropertyDescriptor;
import com.ihr360.alps.PropertyType;
import com.ihr360.alps.ServiceDescriptor;
import com.ihr360.alps.api.ServiceProvider;
import com.ihr360.authority.sharing.annonation.AccessControl;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.lang.StringUtils;
import com.ihr360.payroll.config.SalaryTaxEntity;
import com.ihr360.payroll.config.SalaryTaxTableEntity;
import com.ihr360.payroll.dao.SalaryTaxTableRepository;
import com.ihr360.payroll.model.dto.salary.tax.SalaryTaxTable;
import com.ihr360.payroll.service.payroll.convertor.SalaryTaxTableConvertor;
import com.ihr360.payroll.service.salarytax.SalaryTaxTableService;
import com.ihr360.rest.reposity.SimpleRestReposity;
import com.ihr360.rest.service.AbstractSimpleRestServiceImpl;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author richey
 */
@Service
@AccessControl(disableAll = true)
public class SalaryTaxTableServiceImpl extends AbstractSimpleRestServiceImpl<SalaryTaxTable, Long> implements SalaryTaxTableService {

    @Autowired
    private SalaryTaxTableRepository salaryTaxTableReposity;

    @Override
    public SimpleRestReposity<SalaryTaxTable, Long> getSimpleRestReposity() {
        return salaryTaxTableReposity;
    }


    @Autowired
    private SalaryTaxTableConvertor salaryTaxTableConvertor;


    @Override
    public ServiceDescriptor serviceDescriptor() {

        List<SalaryTaxTable> salaryTaxTables = (List<SalaryTaxTable>) salaryTaxTableReposity.findAll();
        Map<String, List<SalaryTaxTable>> codeMap = salaryTaxTables.stream()
                .filter(s -> (s.getType() != null && StringUtils.isNotEmpty(s.getType().getCode())))
                .collect(Collectors.groupingBy(s -> s.getType().getCode()));

        List<PropertyDescriptor> propertyDescriptors = Lists.newArrayListWithCapacity(codeMap.size());
        codeMap.forEach((code, taxTable) -> {
            propertyDescriptors.add(PropertyDescriptor.builder()
                    .id(code)
                    .name(taxTable.get(0).getType().getName())
                    .type(PropertyType.String)
                    .build());
        });

        return ServiceDescriptor.builder()
                .serviceId(ServiceProvider.SERVICE_ID.SYS_TAX_TABLE)
                .name("税表")
                .path("/alps/" + ServiceProvider.SERVICE_ID.SYS_TAX_TABLE)
                .descriptors(propertyDescriptors)
                .build();

    }

    @Override
    public SalaryTaxTableEntity getCurrentEffective(Long id) {
        ValidationUtils.rejectIfEmpty(id, null, "请求参数异常，id不能为空！");
        SalaryTaxTable salaryTaxTable = salaryTaxTableReposity.findOne(id);
        ValidationUtils.rejectIfEmpty(salaryTaxTable, null, "税表不存在！");
        SalaryTaxTableEntity entity = salaryTaxTableConvertor.convertDtoToEntity(salaryTaxTable);
        List<SalaryTaxEntity> salaryTaxEntities = entity.getSalaryTaxes();

        if (CollectionUtils.isEmpty(salaryTaxEntities)) {
            return entity;
        }
        salaryTaxEntities = salaryTaxEntities.stream().filter(s -> s.isCurrentEffective()).collect(Collectors.toList());
        entity.setSalaryTaxes(salaryTaxEntities);
        return entity;
    }


}
